package com.qyhl.cloud.webtv.module_integral.wallet.goldcoin;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Shader;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

public class GlodForm extends View {
    /**
     * 控件宽度
     */
    private int width;
    /**
     * 控件高度
     */
    private int height;
    /**
     * 公用画笔
     */
    private Paint mainPaint = new Paint();
    /**
     * 字体居中偏移量
     */
    private float textOffset;
    /**
     * 曲线下方背景画笔
     */
    private Paint curveDownBgPaint = new Paint();
    /**
     * 分割线数值
     */
    private int caseValue;
    /**
     * 底部横线X起始坐标
     */
    private int offsetStartX;
    /**
     * 底部横线X截止坐标
     */
    private int offsetEndX;
    /**
     * 底部横线Y截止坐标
     */
    private int offsetEndY;
    /**
     * 顶部横线Y起始坐标
     */
    private int offsetStartY;
    /**
     * 每一格数值高度
     */
    private int verticalCaseHeight;
    /**
     * 每一格日期宽度
     */
    private int horizontalCaseWeight;
    /**
     * 点坐标路径
     */
    private Path path = new Path();
    /**
     * 点坐标集合
     */
    private Point[] points;

    //------------------------------------可设置-----------------------------------------------
    /**
     * 点，线颜色
     */
    private final int GULES = 0xffE53935;
    /**
     * 字体颜色
     */
    private final int GRAY_TEXT = 0xff1D1D1D;
    /**
     * 分割线颜色
     */
    private final int GRAY_LINE = 0x427c7c7c;
    /**
     * 竖向分为多少格
     */
    private int verticalCase = 4;
    /**
     * 数值列表
     */
    private int[] values;
    /**
     * 日期列表
     */
    private String[] times;
    /**
     * 字体大小
     */
    private float textsize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
            12, getResources().getDisplayMetrics());

    public GlodForm(Context context) {
        this(context, null);
    }

    public GlodForm(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GlodForm(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = getMeasureSize(widthMeasureSpec, 400);
        height = getMeasureSize(heightMeasureSpec, 250);
        setMeasuredDimension(width, height);
    }

    private int getMeasureSize(int measureSpec, int value) {
        int measureSize;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            measureSize = size;
        } else {
            measureSize = Math.max(getSuggestedMinimumWidth(), value);
        }
        return measureSize;
    }

    private void init() {
        curveDownBgPaint.setStyle(Paint.Style.FILL);
        curveDownBgPaint.setAntiAlias(true);

        mainPaint.setStrokeWidth(2);
        mainPaint.setAntiAlias(true);
        mainPaint.setTextSize(textsize);
        Paint.FontMetrics fontMetrics = mainPaint.getFontMetrics();
        //为基线到字体上边框的距离fontMetrics.top
        //为基线到字体下边框的距离fontMetrics.bottom
        textOffset = fontMetrics.top / 2 + fontMetrics.bottom / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        computeValues();
        computeCoordinate();
        computePoint();

        drawValues(canvas);
        drawLine(canvas);
        drawDivider(canvas);
        drawTimes(canvas);
        drawPointAndLine(canvas);
        drawShadow(canvas);
    }

    private void computeCoordinate() {
        //初始化各种坐标
        int paddingleft = (int) mainPaint.measureText(String.valueOf(caseValue * (verticalCase - 1)));
        //文字长度+左边距10dp
        offsetStartX = paddingleft + 20;
        //右边距18dp
        offsetEndX = width - 36;
        //上边距22dp
        offsetStartY = 44;
        //下边距38dp
        offsetEndY = height - 75;
        verticalCaseHeight = (offsetEndY - offsetStartY) / verticalCase;
        //宽度计算多减掉一个控件右边距15
        horizontalCaseWeight = (width - offsetStartX - 15) / times.length;
    }

    /**
     * 数值计算
     */
    private void computeValues() {
        //计算平均每一个格表示数值
        int max = values[0];
        for (int i = values.length - 1; i > 0; i--) {
            if (values[i] > max) {
                max = values[i];
            }
        }
        if (max <= 0) {
            caseValue = 0;
        } else {
            caseValue = max / verticalCase;
            if (caseValue < 6) {
                caseValue = 5;
            } else if (caseValue < 11) {
                caseValue = 10;
            } else {
                caseValue = (caseValue / 10 + 1) * 10;
            }
        }
    }

    /**
     * 计算点坐标
     */
    private void computePoint() {
        for (int i = 0; i < values.length; i++) {
            if (caseValue == 0) {
                //坐标点全为0
                points[i] = new Point(offsetStartX + horizontalCaseWeight * i + horizontalCaseWeight / 2,
                        offsetEndY);
            } else {
                //能整除部分占多少格
                int numcase = values[i] / caseValue;
                //超出数值
                int beyond = values[i] % caseValue;
                //超出数值所占高度
                int beyondheight = 0;
                if (beyond != 0) {
                    beyondheight = (int) ((float) beyond / (float) caseValue * (float) verticalCaseHeight);
                }
                points[i] = new Point(offsetStartX + horizontalCaseWeight * i + horizontalCaseWeight / 2,
                        offsetEndY - (beyondheight + numcase * verticalCaseHeight));
            }
        }
        path.reset();
        path.moveTo(points[0].x, points[0].y);
        for (int i = 1; i < points.length; i++) {
            path.lineTo(points[i].x, points[i].y);
        }
    }

    /**
     * 画数值
     */
    private void drawValues(Canvas canvas) {
        //设置画笔
        mainPaint.setColor(GRAY_TEXT);
        mainPaint.setTextAlign(Paint.Align.RIGHT);
        //画值
        if (caseValue == 0) {
            //坐标点全为0
            canvas.drawText(String.valueOf(0), offsetStartX - 3,
                    offsetEndY - textOffset, mainPaint);
        } else {
            for (int i = 0; i < verticalCase; i++) {
                //字体X坐标额外向左偏移3
                canvas.drawText(String.valueOf(caseValue * i), offsetStartX - 5,
                        offsetEndY - i * verticalCaseHeight - textOffset, mainPaint);
            }
        }
    }

    /**
     * 画时间
     */
    private void drawTimes(Canvas canvas) {
        //设置画笔
        mainPaint.setColor(GRAY_TEXT);
        mainPaint.setTextAlign(Paint.Align.CENTER);
        for (int i = 0; i < times.length; i++) {
            //字体Y坐标额外向下偏移20
            canvas.drawText(times[i], offsetStartX + horizontalCaseWeight * i + horizontalCaseWeight / 2,
                    offsetEndY - textOffset + 30, mainPaint);
        }
    }

    /**
     * 画线
     */
    private void drawLine(Canvas canvas) {
        mainPaint.setColor(GULES);
        mainPaint.setStrokeWidth(3);
        //竖线上方超出5
        canvas.drawLine(offsetStartX, offsetStartY - 5, offsetStartX, offsetEndY, mainPaint);
        canvas.drawLine(offsetStartX, offsetEndY, offsetEndX, offsetEndY, mainPaint);
    }

    /**
     * 画分割线
     */
    private void drawDivider(Canvas canvas) {
        mainPaint.setColor(GRAY_LINE);
        mainPaint.setStrokeWidth(2);
        for (int i = 0; i < verticalCase; i++) {
            canvas.drawLine(offsetStartX, offsetEndY - verticalCaseHeight * (i + 1)
                    , offsetEndX, offsetEndY - verticalCaseHeight * (i + 1), mainPaint);
        }
    }

    /**
     * 画点和线
     */
    private void drawPointAndLine(Canvas canvas) {
        mainPaint.setColor(GULES);
        mainPaint.setStyle(Paint.Style.FILL);
        for (int i = 0; i < points.length; i++) {
            canvas.drawCircle(points[i].x, points[i].y, 6, mainPaint);
        }
        mainPaint.setStyle(Paint.Style.STROKE);
        mainPaint.setStrokeWidth(4);
        canvas.drawPath(path, mainPaint);
    }

    /**
     * 画点和线
     */
    private void drawShadow(Canvas canvas) {
        mainPaint.setStyle(Paint.Style.FILL);
        //将path连接成封闭图形
        path.lineTo(points[points.length - 1].x, offsetEndY);
        path.lineTo(points[0].x, offsetEndY);
        curveDownBgPaint.setShader(new LinearGradient(0, 0, 0, height,
                new int[]{0xffE53935, 0xffffff}, null, Shader.TileMode.CLAMP));
        canvas.drawPath(path, curveDownBgPaint);
    }

    public void setValues(int[] values) {
        this.values = values;
        points = new Point[values.length];
    }

    public void setTimes(String[] times) {
        this.times = times;
    }

    public void update() {
        invalidate();
    }
}
